﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;
using prjBULHRM;
using System.IO;
namespace prjQuanLyNhanSu
{
    public partial class frmLuoiNhanVien : DevExpress.XtraEditors.XtraForm
    {
        public frmLuoiNhanVien()
        {
            InitializeComponent();
        }
        int STT = 0;
        clsBULHRNhanVien clsNhanVien = new clsBULHRNhanVien();
        clsBULTT_PhongBan clsPhongBan = new clsBULTT_PhongBan();

        public void LoadDuLieu()
        {
            dgrNhanVien.DataSource = clsNhanVien.SelectAll();

        }
        public void LoadTree()
        {
            DataTable tbl = clsPhongBan.SelectAll();
            for (int i = 0; i < tbl.Rows.Count; i++)
            {
                DataRow drw = tbl.Rows[i];
                if (drw["PhongBanCapTren"].ToString() == drw["PhongBan_ID"].ToString())
                {
                    TreeNode nCha = new TreeNode();
                    nCha.Text = drw["Name"].ToString();
                    nCha.Tag = drw["PhongBan_ID"].ToString();
                    tbl.Rows.Remove(drw);
                    i--;
                    LayNodeCha(tbl, nCha);
                    trePhongBan.Nodes.Add(nCha);
                }
            }
        }

        private void LayNodeCha(DataTable tbl, TreeNode nodeCha)
        {
            string sMaPB = nodeCha.Tag.ToString();
            TreeNode nodeCon;
            int dung = 1;
            //kiem tra xem node cha nay co node con k?
            for (int i = 0; i < tbl.Rows.Count; i++)
            {
                DataRow drw = tbl.Rows[i];
                if (drw["PhongBanCapTren"].ToString() == sMaPB)
                {
                    nodeCon = new TreeNode();
                    nodeCon.Text = drw["Name"].ToString();
                    nodeCon.Tag = drw["PhongBan_ID"].ToString();
                    tbl.Rows.Remove(drw);
                    i--;
                    LayNodeCha(tbl, nodeCon);
                    nodeCha.Nodes.Add(nodeCon);
                }
            }
        }

        private void frmLuoiNhanVien_Load(object sender, EventArgs e)
        {
            LoadTree();
            LoadDuLieu();
        }

        private void KhoiTaoDuLieuMang(TreeNode node,ref string str)
        {
            str += ","+node.Tag.ToString();
            if (node.Nodes.Count==0)//neu la node con cuoi cung
            {
                return;
            }
            //de quy cho node con;))
            foreach (TreeNode nCon in node.Nodes)
            {
                KhoiTaoDuLieuMang(nCon, ref str);
            }
        }
        //public string _nodeID;
        private void trePhongBan_AfterSelect(object sender, TreeViewEventArgs e)
        {
            DataTable tbl = clsNhanVien.SelectAll();
            TreeNode nodeTam = new TreeNode();
            string[] mang=new string[100];
            //Khoi tao du lieu cho mang
            string str = "";
            KhoiTaoDuLieuMang(trePhongBan.SelectedNode, ref str);
            str = str.Remove(0, 1);
            //MessageBox.Show(str);
            mang = str.Split(',');
            
            if (trePhongBan.SelectedNode != null)
            {

                bool bThuoc = false;
                for (int i = 0; i < tbl.Rows.Count; i++)//Loc nhan vien
                {
                    bThuoc = false;
                    DataRow row = tbl.Rows[i];
                    for (int j = 0; j < mang.Length; j++)//Loc mang ma phong ban
                    {
                        if ((mang[j] == row["PhongBan"].ToString()))// node khong co node cha thi no la node cha.
                        {
                            bThuoc = true;
                            break;
                        }
                    }
                    if (bThuoc == false)//Neu nhan vien ko co trong mang phong ban
                    {
                        tbl.Rows.RemoveAt(i);
                        i--;
                    }                   
                }
                dgrNhanVien.DataSource = tbl;                
                //Kiem tra node cha con lam gi?
                                 
                   
            }     
        }

        private void trePhongBan_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            //DataTable tbl = clsNhanVien.SelectAll();
            //TreeNode nodeTam = new TreeNode();
            //if (trePhongBan.SelectedNode != null)
            //{
            //    if (trePhongBan.SelectedNode.Parent == null)// node khong co node cha thi no la node cha.
            //    {
            //        nodeTam = trePhongBan.SelectedNode;
            //        string sma = nodeTam.Tag.ToString();
            //        for (int i = 0; i < tbl.Rows.Count; i++)
            //        {
            //            foreach (DataRow row in tbl.Rows)
            //            {
            //                if (row["PhongBan"].ToString() == sma)
            //                {
            //                    dgrNhanVien.DataSource = clsNhanVien.SelectAll();
            //                }
            //            }
            //        }
            //    }
            //}            
        }

        private void advDgrid_DoubleClick(object sender, EventArgs e)
        {
            
        }

        private void dgrNhanVien_DoubleClick(object sender, EventArgs e)
        {
            int i = advDgrid.GetFocusedDataSourceRowIndex();

            DataRow row = advDgrid.GetDataRow(i);
            frmNhanVien frm = new frmNhanVien();
            frm.txtMaNV.Text = row["NhanVien_ID"].ToString();
            frm.txtHoTenNV.Text = row["Name"].ToString();
            frm.txtBiDanh.Text = row["BiDanh"].ToString();
            frm.chkNu.Checked= bool.Parse(row["Sex"].ToString());
            frm.dNgaySinhNV.DateTime =DateTime.Parse(row["Birth"].ToString()) ;
            frm.txtDiDongNV.Text = row["CellPhone"].ToString();
            frm.txtDTNhaNV.Text = row["Phone"].ToString();
            frm.cboQuocTich.SelectedValue = row["QuocTich"].ToString();
            frm.cboTonGiao.SelectedValue = row["TonGiao"].ToString();
            string []strMaMay=row["MaMay"].ToString().Split('-');
            frm.txtMaMay1.Text = strMaMay[0];
            frm.txtMaMay2.Text = strMaMay[1];
            frm.txtNoiSinh.Text = row["BirthAdress"].ToString();
            frm.txtThuongTru.Text = row["Adress"].ToString();
            frm.txtTramTru.Text = row["T_Adress"].ToString();
            frm.cboTinhTrangHonNhan.SelectedIndex =int.Parse(row["Maried"].ToString()) ;
            frm.txtCMNDNV.Text = row["CMND"].ToString();
            frm.dNgayCapCMND.Text = row["CMNDDay"].ToString();
            frm.txtNoiCapCMND.Text = row["CMNDAdress"].ToString();
            frm.txtEmail.Text = row["Email"].ToString();
            string sHinh=row["Image"].ToString();
            Byte[] bt =(Byte[]) row["Image"];
            //for (int k = 0; k < shinh.length;k++ )
            //{
            //    bt[i] = byte.parse(shinh[k].tostring());
            //}
            MemoryStream rd=new MemoryStream(bt);
            //rd.Read(bt,0,bt.Length);
            Image a = Image.FromStream(rd);
            frm.HinhNV.Image = Image.FromStream((Stream)rd);
            frm.cboLoaiNV.SelectedValue = row["LoaiNhanVien"].ToString();
            frm.cboPhongBan.SelectedValue = row["PhongBan"].ToString();
            frm.cboCongViec.SelectedValue = row["CongViec"].ToString();
            frm.txtLuongCoBan.Text = row["LuongCB"].ToString();
            frm.cboChucVu.SelectedValue = row["ChucVu"].ToString();
            //frm.cboCa.SelectedValue = row["CaLamViec"].ToString();
            frm.dNgayVaoLam.DateTime =DateTime.Parse(row["NgayVaoLam"].ToString());
            frm.txtSoNgayPhep.Text =row["SoNgayPhep"].ToString();
            frm.txtHeSoLuong.Text = row["HeSoLuong"].ToString();
            frm.txtSoLaoDong.Text = row["SoLaoDong"].ToString();
            frm.dNgayCapSLD.DateTime = DateTime.Parse(row["NgayCapSLD"].ToString());
            frm.txtNoiCapSLD.Text = row["NoiCapSLD"].ToString();
            frm.rtxtGhiChu.Text = row["GhiChu"].ToString();

            frm.txtSoTheDoan.Text = row["SoTheDoan"].ToString();
            frm.dNgayCapSTDoan.DateTime =DateTime.Parse(row["NgayCapSTD"].ToString());
            frm.txtNoiCapSTDoan.Text = row["NoiCapSTD"].ToString();
            frm.txtSoTheDang.Text = row["SoTheDang"].ToString();
            frm.dNgayCapSTDang.DateTime = DateTime.Parse(row["NgayCapSTDang"].ToString());
            frm.txtNoiCapSTDang.Text = row["NoiCapSTDang"].ToString();
            frm.txtSoHC.Text = row["SoHoChieu"].ToString();
            frm.dNgayBDHC.DateTime =DateTime.Parse( row["NgayBDSHC"].ToString());
            frm.dNgayKTHC.DateTime = DateTime.Parse(row["NgayKTSHC"].ToString());
            frm.txtNoiCapSHC.Text = row["NoiCapSHC"].ToString();
            frm.txtSoBHYT.Text = row["SoBaoHiemYT"].ToString();
            frm.dNgayCapBHYT.DateTime = DateTime.Parse(row["NgayCapSBHYT"].ToString());
            frm.txtNoiCapBHYT.Text = row["NoiCapSBHYT"].ToString();
            frm.txtBHXH.Text = row["SoBaoHiemXH"].ToString();
            frm.dNgayCapBHXH.DateTime = DateTime.Parse(row["NgayCapSBHXH"].ToString());
            frm.txtNoiCapBHXH.Text = row["NoiCapSBHXH"].ToString();
            frm.txtSoNganHang.Text = row["BankAcount"].ToString();
            frm.txtTenNganHang.Text = row["Bank"].ToString();

            frm.txtMaNVQH.Text = row["NhanVien_ID"].ToString();
            frm.txtTenNVQH.Text = row["Name"].ToString();
            			
            frm.ShowDialog();
        }

        private void btnThem_CheckedChanged(object sender, EventArgs e)
        {
            frmNhanVien frm = new frmNhanVien();
            frm.txtMaNV.Text = "";
            frm.txtHoTenNV.Text = "";
            frm.txtBiDanh.Text = "";
            frm.dNgaySinhNV.Text =DateTime.Now.ToString();
            frm.txtDiDongNV.Text = "";
            frm.txtDTNhaNV.Text = "";
            frm.chkNu.Checked = false;
            frm.txtLuongCoBan.Text = "";
            frm.txtNoiSinh.Text = "";
            frm.txtThuongTru.Text = "";
            frm.txtTramTru.Text = "";
            frm.txtCMND.Text = "";
            frm.dNgayCapCMND.Text = DateTime.Now.ToString();
            frm.txtNoiCapCMND.Text = "";
            frm.txtEmail.Text = "";
            frm.txtMaMay1.Text = "";
            frm.txtMaMay2.Text = "";
            frm.txtLuongCoBan.Text = "";
            frm.dNgayVaoLam.Text = DateTime.Now.ToString();
            frm.txtSoNgayPhep.Text = "";
            frm.txtHeSoLuong.Text = "";
            frm.txtSoLaoDong.Text = "";
            frm.dNgayCapSLD.Text = DateTime.Now.ToString();
            frm.txtNoiCapSLD.Text = "";
            frm.rtxtGhiChu.Text = "";
            frm.cboCa.SelectedIndex = -1;
            frm.cboChucVu.SelectedIndex = -1;
            frm.cboCongViec.SelectedIndex = -1;
            frm.cboDanToc.SelectedIndex = -1;
            frm.cboLoaiNV.SelectedIndex = -1;
            frm.cboLoaiQuanHe.SelectedIndex = -1;
            frm.cboPhongBan.SelectedIndex = -1;
            frm.cboQuocTich.SelectedIndex = -1;
            frm.cboTonGiao.SelectedIndex = -1;
            frm.cboTinhTrangHonNhan.SelectedIndex = -1;
            //Xa hoi
            frm.txtSoTheDoan.Text = "";
            frm.dNgayCapSTDoan.Text = DateTime.Now.ToString();
            frm.txtNoiCapSTDoan.Text = "";
            frm.txtSoTheDang.Text = "";
            frm.dNgayCapSTDang.Text = DateTime.Now.ToString();
            frm.txtNoiCapSTDang.Text = "";
            frm.txtSoHC.Text = "";
            frm.dNgayBDHC.Text = "";
            frm.dNgayKTHC.Text = DateTime.Now.ToString();
            frm.txtNoiCapSHC.Text = "";
            frm.txtBHXH.Text = "";
            frm.txtNoiCapBHXH.Text = "";
            frm.dNgayCapBHXH.Text = DateTime.Now.ToString();
            frm.txtSoBHYT.Text = "";
            frm.dNgayCapBHYT.Text = DateTime.Now.ToString();
            frm.txtNoiCapBHYT.Text = "";
            frm.txtSoNganHang.Text = "";
            frm.txtTenNganHang.Text = "";
            //btnSua.Enabled = false;
            //btnXoa.Enabled = false;
            frm.Sua = false;
            frm.ShowDialog();
        }

        private void btnSua_CheckedChanged(object sender, EventArgs e)
        {
            int i = advDgrid.GetFocusedDataSourceRowIndex();

            DataRow row = advDgrid.GetDataRow(i);
            frmNhanVien frm = new frmNhanVien();
            frm.txtMaNV.Enabled = false;
            
			

            frm.txtMaNV.Text = row["NhanVien_ID"].ToString();
            frm.txtHoTenNV.Text = row["Name"].ToString();
            frm.txtBiDanh.Text = row["BiDanh"].ToString();
            frm.chkNu.Checked = bool.Parse(row["Sex"].ToString());
            frm.dNgaySinhNV.DateTime = DateTime.Parse(row["Birth"].ToString());
            frm.txtDiDongNV.Text = row["CellPhone"].ToString();
            frm.txtDTNhaNV.Text = row["Phone"].ToString();
            frm.cboQuocTich.SelectedValue = row["QuocTich"].ToString();
            frm.cboTonGiao.SelectedValue = row["TonGiao"].ToString();
            string[] strMaMay = row["MaMay"].ToString().Split('-');
            frm.txtMaMay1.Text = strMaMay[0];
            frm.txtMaMay2.Text = strMaMay[1];
            frm.txtNoiSinh.Text = row["BirthAdress"].ToString();
            frm.txtThuongTru.Text = row["Adress"].ToString();
            frm.txtTramTru.Text = row["T_Adress"].ToString();
            frm.cboTinhTrangHonNhan.SelectedIndex = int.Parse(row["Maried"].ToString());
            frm.txtCMNDNV.Text = row["CMND"].ToString();
            frm.dNgayCapCMND.Text = row["CMNDDay"].ToString();
            frm.txtNoiCapCMND.Text = row["CMNDAdress"].ToString();
            frm.txtEmail.Text = row["Email"].ToString();
            string sHinh = row["Image"].ToString();
            Byte[] bt = (Byte[])row["Image"];
            MemoryStream rd = new MemoryStream(bt);
            Image a = Image.FromStream(rd);
            frm.HinhNV.Image = Image.FromStream((Stream)rd);
            frm.cboLoaiNV.SelectedValue = row["LoaiNhanVien"].ToString();
            frm.cboPhongBan.SelectedValue = row["PhongBan"].ToString();
            frm.cboCongViec.SelectedValue = row["CongViec"].ToString();
            frm.txtLuongCoBan.Text = row["LuongCB"].ToString();
            frm.cboChucVu.SelectedValue = row["ChucVu"].ToString();
            //frm.cboCa.SelectedValue = row["CaLamViec"].ToString();
            frm.dNgayVaoLam.DateTime = DateTime.Parse(row["NgayVaoLam"].ToString());
            frm.txtSoNgayPhep.Text = row["SoNgayPhep"].ToString();
            frm.txtHeSoLuong.Text = row["HeSoLuong"].ToString();
            frm.txtSoLaoDong.Text = row["SoLaoDong"].ToString();
            frm.dNgayCapSLD.DateTime = DateTime.Parse(row["NgayCapSLD"].ToString());
            frm.txtNoiCapSLD.Text = row["NoiCapSLD"].ToString();
            frm.rtxtGhiChu.Text = row["GhiChu"].ToString();

            frm.txtSoTheDoan.Text = row["SoTheDoan"].ToString();
            frm.dNgayCapSTDoan.DateTime = DateTime.Parse(row["NgayCapSTD"].ToString());
            frm.txtNoiCapSTDoan.Text = row["NoiCapSTD"].ToString();
            frm.txtSoTheDang.Text = row["SoTheDang"].ToString();
            frm.dNgayCapSTDang.DateTime = DateTime.Parse(row["NgayCapSTDang"].ToString());
            frm.txtNoiCapSTDang.Text = row["NoiCapSTDang"].ToString();
            frm.txtSoHC.Text = row["SoHoChieu"].ToString();
            frm.dNgayBDHC.DateTime = DateTime.Parse(row["NgayBDSHC"].ToString());
            frm.dNgayKTHC.DateTime = DateTime.Parse(row["NgayKTSHC"].ToString());
            frm.txtNoiCapSHC.Text = row["NoiCapSHC"].ToString();
            frm.txtSoBHYT.Text = row["SoBaoHiemYT"].ToString();
            frm.dNgayCapBHYT.DateTime = DateTime.Parse(row["NgayCapSBHYT"].ToString());
            frm.txtNoiCapBHYT.Text = row["NoiCapSBHYT"].ToString();
            frm.txtBHXH.Text = row["SoBaoHiemXH"].ToString();
            frm.dNgayCapBHXH.DateTime = DateTime.Parse(row["NgayCapSBHXH"].ToString());
            frm.txtNoiCapBHXH.Text = row["NoiCapSBHXH"].ToString();
            frm.txtSoNganHang.Text = row["BankAcount"].ToString();
            frm.txtTenNganHang.Text = row["Bank"].ToString();

            frm.txtMaNVQH.Text = row["NhanVien_ID"].ToString();
            frm.txtTenNVQH.Text = row["Name"].ToString();

            frm.Sua = true;
            frm.ShowDialog();
        }
    }
}